package com.dingreading.cloud.app.util;

import com.dingreading.cloud.app.entity.MemberActionLog;
import com.dingreading.cloud.app.entity.Members;
import com.dingreading.cloud.app.entity.Relatives;
import com.dingreading.cloud.app.service.MemberActionLogService;
import com.dingreading.cloud.app.service.RelativesService;
import com.dingreading.cloud.app.util.wxtemplate.TemplateMessUtils;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.WxGzhUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class WxTemplateUtil {

    public static WxTemplateUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    @Resource
    private RelativesService relativesService;
    @Resource
    private MemberActionLogService memberActionLogService;

    /**
     * 签到签退发送微信消息给亲属
     *
     * @param members    会员
     * @param duration   在店时长(秒)
     * @param storeName  门店名
     * @param signInType 类型 1.签到 2.签退
     */
    public static void wxSendToRelatives(Members members, Long duration, String storeName, Integer signInType, String token) {
        if (members != null) {
            String time = DateUtil.getNowDatetimeStr();
            List<Relatives> relativesList = util.relativesService.getByMemberIdAndGzhOpenIdAndGzhPush(members.getId(), 1);
            if (CollectionUtils.isNotEmpty(relativesList)) {
                String timeYmd = DateUtil.getNowDateStr();
                if (signInType == EnumType.SignInType.SIGN_IN.getType()) {
                    //签到
                    String stringFormat = "签到成功";
                    stringFormat = String.format(stringFormat, members.getFullName());

                    for (Relatives relatives : relativesList) {
                        String build = TemplateMessUtils.signIn(relatives.getGzhOpenId(), members.getFullName(), storeName, time, stringFormat);
                        // 发送微信模板消息
                        WxGzhUtil.sendTemplate(build, token);
                    }
                } else if (signInType == EnumType.SignInType.SIGN_OUT.getType()) {
                    //签退
                    String durationStr = getDate(duration);
                    String stringFormat = "";
                    List<MemberActionLog> list = util.memberActionLogService.getMemberBookActionLogGroupBookId(
                            members.getStoreId(), members.getId(), timeYmd);
                    if (CollectionUtils.isNotEmpty(list)) {
                        String bookStr = getBookStr(list);
                        stringFormat = "%s家长您好，孩子今天在书房约%s。%s今天来到书房后，阅读了%s。叮叮书房有经典，在叮叮，好读书！";
                        stringFormat = String.format(stringFormat, members.getFullName(), durationStr, members.getFullName(), bookStr);
                    } else {
                        stringFormat = "%s家长您好，孩子今天在书房约%s。叮叮书房有经典，在叮叮，好读书！";
                        stringFormat = String.format(stringFormat, members.getFullName(), durationStr);
                    }
                    // 发送微信模板消息
                    for (Relatives relatives : relativesList) {
                        String build = TemplateMessUtils.signOut(relatives.getGzhOpenId(), members.getFullName(), storeName, time, stringFormat);
                        WxGzhUtil.sendTemplate(build, token);
                    }
                } else {

                }
            }
        }
    }

    /**
     * 获取在店时间
     *
     * @param seconds 待转换的秒
     * @return str
     */
    public static String getDate(Long seconds) {
        Long h = seconds / 3600;
        Long m = (seconds % 3600) / 60;
        Long s = (seconds % 3600) % 60;

        if (h > 0) {
            return h + "小时" + m + "分钟";
        } else {
            return m + "分钟";
        }
    }

    /**
     * 获取备注信息的书籍字符串
     *
     * @param list 阅读记录
     * @return str
     */
    public static String getBookStr(List<MemberActionLog> list) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            MemberActionLog log = list.get(i);
            if (log == null) continue;
            if (StringUtils.isBlank(log.getBookName())) continue;
            String bookName = log.getBookName();
            if (bookName.length() > 10)
                bookName = bookName.substring(0, 10).concat("..");
            if (i == 0) {
                builder.append("《".concat(bookName).concat("》"));
            } else {
                builder.append("、《".concat(bookName).concat("》"));
            }
            if (i == 1) break;
        }
        if (list.size() > 2)
            builder.append("等");
        builder.append(list.size()).append("本书");
        return builder.toString();
    }

}
